package com.jrx.anytxn.accounting.job.voucherFile.config;

import com.jrx.anytxn.accounting.config.SftpConfig;
import com.jrx.anytxn.accounting.config.VoucherFileConfig;
import com.jrx.anytxn.accounting.constant.AccountBizConstant;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.common.uitl.SftpUtils;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;


/**
 * 传票文件上传
 */
@Configuration
@EnableConfigurationProperties({SftpConfig.class,VoucherFileConfig.class})
public class VoucherFileUploadConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(VoucherFileUploadConfiguration.class);


    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private SftpConfig sftpConfig;
    @Autowired
    private VoucherFileConfig voucherFileConfig;
    @Autowired
    private IOrganizationTableService organizationTableService;

    @Bean
    public Job voucherFileUploadJob() {
        return jobBuilderFactory.get("voucherFileUploadJob")
                .start(voucherFileUploadStep())
                .build();
    }

    @Bean
    public Step voucherFileUploadStep() {
        return stepBuilderFactory.get("voucherFileUploadStep")
                .tasklet(voucherFileUploadTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet voucherFileUploadTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("传票文件上传开始。。。。。。");

            String basePath = voucherFileConfig.getVoucherPath();

            PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(AccountBizConstant.ORG);
            String today = DateUtils.format(org.getToday(),"yyyyMMdd");

            String filePath = basePath + org.getTenantId() + File.separator + today + File.separator;

            File file = new File(filePath);
            File[] tempList = file.listFiles();

            //1 获取连接
            SftpUtils.sftpConnect(sftpConfig.getUsername(),sftpConfig.getPassword(),sftpConfig.getHost(),sftpConfig.getPort());

            //上传路径
            String directory = sftpConfig.getUploadPath() + org.getTenantId() + "/" + today;

            //2 检查创建目录
            SftpUtils.createDir(directory);


            String txtName = AccountBizConstant.VOUCHER_FILE_TYPE + "_" +today + ".txt";
            //3 先上传TXT文件，再上传OK文件
            for(File uploadFile : tempList){
                String filename = uploadFile.getName();
                if(txtName.equals(filename)){
                    SftpUtils.sftpUpload(uploadFile);
                }
            }
            logger.info("传票txt文件上传完成。。。。。。");

            String okFileName = AccountBizConstant.VOUCHER_FILE_TYPE + "_" +today + ".ok";
            for(File uploadFile : tempList){
                String filename = uploadFile.getName();
                if(okFileName.equals(filename)){
                    SftpUtils.sftpUpload(uploadFile);
                }
            }
            logger.info("传票单个ok文件上传完成。。。。。。");

            //关闭连接
            SftpUtils.disSftpConnect();

            logger.info("传票文件上传结束。。。。。。");
            return RepeatStatus.FINISHED;
        };
    }
}
